package d3_com.itheima.stringbuilder;

import java.util.StringJoiner;

public class Test {
    public static void main(String[] args) {
        //StringBuilder 线程不安全 ; StringBuffer 线程安全。
        //设计一个方法，用于返回任意整型数组的内容，要求返回的数组
        System.out.println(getData(new int[]{1, 2, 3}));
        System.out.println(getData2(new int[]{1, 2, 3}));
    }
    public static String getData(int[] arr) {
        if (arr == null) {
            return null;
        }

        StringBuilder sb = new StringBuilder();
        sb.append("[");
        for (int i = 0; i < arr.length; i++) {
            if (i == arr.length - 1) {
                sb.append(arr[i]);
            } else {
                sb.append(arr[i]).append(",");
            }
        }
        sb.append("]");
        return sb.toString();
    }
    //StringJoiner
    public static String getData2(int arr[]) {
        // 目标:掌握StringJoiner的使用。
        //StringJoiners=new StringJoiner("，");// 间隔符!
        StringJoiner s = new StringJoiner( ",","[","]");// 间隔符!
        for (int i = 0; i < arr.length; i++) {
            s.add(arr[i]+"");
        }
        return s.toString();
    }

}
